﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;

namespace iLangl.BusinessModel.DAL.XMLStorage
{
	public class TranslationProviderDAL : BaseStorageDAL, ITranslationProviderDAL
	{
		public String GetTranslationProviderIdForCompany(String companyId)
		{
			foreach (dtoXmlStorage.TPCompaniesMappingRow companymapping in Storage.TPCompaniesMapping)
			{
				if (String.Compare(companymapping.CompanyId.Trim(), companyId.Trim(), true) == 0)
				{
					return companymapping.TpId;
				}
			}


			foreach (dtoXmlStorage.TranslationProviderRow tprow in Storage.TranslationProvider)
			{
				if (tprow.IsDefault)
				{
					return tprow.Id;
				}
			}

			//TODO: Generate ALERT HERE - Translation Provider not bounded to Company
			throw new ApplicationException(string.Format("Generate ALERT HERE - Translation Provider not founded to Company with Id='{0}'", companyId));
		}




		public void RegisterTranslationProvider(Company company, IDbTransaction trans)
		{
			dtoXmlStorage.TranslationProviderRow transprovrow = Storage.TranslationProvider.FindById(company.Id);

			bool isNew = (transprovrow == null);

			if (isNew)
			{
				transprovrow = Storage.TranslationProvider.NewTranslationProviderRow();
			}
			transprovrow.Id = company.Id;
			transprovrow.IsDefault = false;

			if (isNew)
			{
				Storage.TranslationProvider.AddTranslationProviderRow(transprovrow);
			}

			
			if (trans == null)
			{
				AcceptChanges();
			}

		}


		public void SetDefaultTranslationProvider(string providerId)
		{
			dtoXmlStorage.TranslationProviderRow transprovrow = Storage.TranslationProvider.FindById(providerId);
			if (transprovrow == null)
				return;

			transprovrow.IsDefault = true;

			AcceptChanges();
		}
	}
}
